//
//  XZShowQRCodeVC.m
//  Teecloud
//
//  Created by PC on 2018/11/7.
//  Copyright © 2018年 小志. All rights reserved.
//

#import "XZShowQRCodeVC.h"
#import "XZSelectSignView.h"
@interface XZShowQRCodeVC ()
@property (weak, nonatomic) IBOutlet UIImageView *QRCodeImg;
@property (weak, nonatomic) IBOutlet UILabel *reasonLabel;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (nonatomic, strong) UIButton *rightBtn;

@property (nonatomic, strong) XZSelectSignView *selectSignView;
@end

@implementation XZShowQRCodeVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].idleTimerDisabled = YES;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].idleTimerDisabled = NO;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.selectSignView hide];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    self.title = @"签到二维码";
    self.reasonLabel.text = self.model.Name;
    self.timeLabel.text = self.model.StartEndTimeName;
    //当前登录的用户姓名
    NSString *name = [XGMArchiverTools cacheForKey:USER_Name];
    if (!kIsEmptyString(name)) {
       self.nameLabel.text = name;
    }else{
      self.nameLabel.text = @"";
    }
    //默认显示签到二维码
    [self.QRCodeImg yy_setImageWithURL:[NSURL URLWithString:self.model.SignInQrUrl] placeholder:DefealtImg];
    //如果签到签退都需要的话 就显示下拉菜单
    if (self.model.IsNeedSignIn && self.model.IsNeedSignOut) {
        UIBarButtonItem *signItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightBtn];
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        spaceItem.width = -15;
        self.navigationItem.rightBarButtonItems = @[spaceItem,signItem];
    }
}

/**
 点击按钮切换下拉列表
 隐藏则显示，反之
 */
- (void)selectQRCodeImg{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    for (UIView *view in rootWindow.subviews) {
        if (view == self.selectSignView) {
            [self.selectSignView hide];
        }else{
            [self.selectSignView show];
        }
    }
}

/**
 选择签到还是签退

 @param type 标识
 */
- (void)selectType:(NSString *)type{
    if ([type isEqualToString:@"signIn"]) {
        [self.rightBtn setTitle:@"签到" forState:UIControlStateNormal];
           [self.QRCodeImg yy_setImageWithURL:[NSURL URLWithString:self.model.SignInQrUrl] placeholder:DefealtImg];
    }else if ([type isEqualToString:@"signOut"]){
        [self.rightBtn setTitle:@"签退" forState:UIControlStateNormal];
           [self.QRCodeImg yy_setImageWithURL:[NSURL URLWithString:self.model.SignOutQrUrl] placeholder:DefealtImg];
    }
    [self.selectSignView hide];
}
-(UIButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(0, 0, 80, 30);
        [_rightBtn setImage:[UIImage imageNamed:@"down-white-64"] forState:UIControlStateNormal];
        [_rightBtn setTitle:@"签到" forState:UIControlStateNormal];
        [_rightBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        _rightBtn.titleLabel.font = [UIFont systemFontOfSize: 15.0];
        _rightBtn.titleLabel.textAlignment = NSTextAlignmentLeft;
        [_rightBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
        [_rightBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 50, 0, 0)];
        [_rightBtn addTarget:self action:@selector(selectQRCodeImg) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}
- (XZSelectSignView *)selectSignView{
    if (!_selectSignView) {
        kWeakObject(self)
        _selectSignView =[XZSelectSignView viewFromXib];
        _selectSignView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, DEVICE_WIDTH, DEVICE_HEIGHT);
        _selectSignView.backgroundColor = kClearColor;
        _selectSignView.signInOrOutBlock = ^(NSString * _Nonnull type) {
            [weakself selectType:type];
        };
    }
    return _selectSignView;
}
@end
